<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/15 15:24
 * Blog：www.myblogs.xyz
 */

namespace app\admin\controller;


use app\common\service\CommonService;
use think\facade\View;

class RequestLog extends Common
{
    public function index()
    {
        $list_where = [];
        // The query time
        $param = input('param.');
        $start_time = empty($param['start_time']) ? '' : str_replace('%2B', ' ', $param['start_time']);
        $end_time = empty($param['end_time']) ? '' : str_replace('%2B', ' ', $param['end_time']);
        if (!empty($start_time)){
            if (strtotime($start_time)){
                $beg = strtotime($start_time);
                if (!empty($end_time) && strtotime($end_time)){
                    $end = strtotime($end_time);
                }else{
                    $date = date('Y-m-d',NEW_TIME) . ' 23:59:59';
                    $end = strtotime($date);
                }
                $list_where['create_time'] = [['egt',$beg],['elt',$end]];
            }
        }
        // keywords
        $keywords = input('param.keywords');
        if (!empty($keywords)){
            $admin_model = new \app\common\model\Admin();
            $admin_id = $admin_model->getField(['account'=>$keywords], 'admin_id');
            if (!empty($admin_id)){
                $list_where['admin_id'] = $admin_id;
            }else{
                $list_where['admin_id'] = 0;
            }
        }
        $request_log = new \app\common\model\RequestLog();
        $data = $request_log->getLists($list_where, 10, 'create_time desc');

        View::assign([
            'keywords'=>$keywords,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'count'=>$data['count'],
            'list'=>$data['list'],
            'page'=>$data['page']
        ]);
        return View::fetch();
    }

    public function deleteLog()
    {
        if (CommonService::delete()){
            $this->returnSuccess('删除成功');
        }
        $this->returnError('删除失败');
    }

    public function delAllLog()
    {
        $request_log = new \app\common\model\RequestLog();
        $rows = $request_log->remove(['log_id', 'gt', 0]);
        if (empty($rows)){
            $this->returnError('删除失败');
        }
        $this->returnSuccess('删除成功', url('RequestLog/index', ['p'=>$this->p]));
    }
}